#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>

using namespace std;

class Solution
{
public:
    vector<int> topKFrequent(vector<int> &nums, int k)
    {
        unordered_map<int, int> m; //[num,count]
        for (auto n : nums)
        {
            m[n]++;
        }
        priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> v;
        for (auto p : m)
        {
            if (v.size() < k)
            {
                v.push({p.second, p.first});
                continue;
            }
            if (p.second > v.top().first)
            {
                v.pop();
                v.push({p.second, p.first});
            }
        }
        vector<int> ans(k, 0);
        for (int i = 0; i < k; i++)
        {
            ans[i] = v.top().second;
            v.pop();
        }
        return ans;
    }
};

int main()
{
    cout << "Code347" << endl;
    Solution s;
    vector<int> nums = {1, 1, 1, 2, 2, 3};
    vector<int> ans = s.topKFrequent(nums, 2);
    for (auto a : ans)
        cout << a << " ";
    return 0;
}